package mall.controller.admin;

import com.google.gson.Gson;
import mall.model.Result;
import mall.model.bo.ChangeOrderBO;
import mall.model.bo.PageOrderBO;
import mall.model.vo.OrderInfoVO;
import mall.model.vo.PageOrderInfoVO;
import mall.model.vo.PageOrdersVO;
import mall.service.OrderService;
import mall.service.OrderServiceImpl;
import mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-05-30 09:07
 *
 * */
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {
    private Gson gson = new Gson();
    private OrderService orderService = new OrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("ordersByPage".equals(action)) {
            ordersByPage(request, response);
        } else if ("changeOrder".equals(action)) {
            changeOrder(request, response);
        }
    }

    /**
    * 修改订单并保存修改
     * 1. 获取请求参数{id: "1", state: 0, spec: 1, num: 1}
     * 2. 修改数据库表相应信息
     * 3. 作出响应{"code":0}
    * */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ChangeOrderBO changeOrderBO = gson.fromJson(requestBody, ChangeOrderBO.class);
        orderService.changeOrder(changeOrderBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 根据条件分页显示订单信息
     * 1. 获取请求参数
     * 2. 处理业务逻辑
     * 3. 作出响应
     * */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        PageOrderBO orderBO = gson.fromJson(requestBody, PageOrderBO.class);
        PageOrdersVO orders = orderService.ordersByPage(orderBO);
        response.getWriter().println(gson.toJson(Result.ok(orders)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("order".equals(action)) {
            order(request, response);
        } else if ("deleteOrder".equals(action)) {
            deleteOrder(request, response);
        }

    }

    /**
     * 删除订单
     * 1. 获取参数id
     * 2. 删除数据库表orders相应数据
     * 3. 作出响应{"code":0}
     * */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        orderService.deleteOrder(id);
        response.getWriter().println(gson.toJson(Result.ok()));

    }

    /**
     * 显示订单信息
     * */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        OrderInfoVO orderInfoVO =  orderService.order(id);
        response.getWriter().println(gson.toJson(Result.ok(orderInfoVO)));
    }
}
